{
 "cells": [
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "4cee4b6384d7adb0"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-11-14T15:14:25.639502Z",
     "start_time": "2024-11-14T15:14:21.495023Z"
    }
   },
   "source": [
    "import torch\n",
    "\n",
    "a = torch.rand(3,2)\n",
    "print(a)\n",
    "b = torch.rand(2)\n",
    "(a@b).shape"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.9220, 0.8074],\n",
      "        [0.3758, 0.5933],\n",
      "        [0.8319, 0.2108]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "torch.Size([3])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-17T05:04:23.971682Z",
     "start_time": "2024-11-17T05:04:20.138099Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "import torch.nn.functional as F\n",
    "\n",
    "# 生成随机输入特征 x 和权重 w\n",
    "x = torch.randn(1, 10)\n",
    "w = torch.randn(2, 10, requires_grad=True)\n",
    "\n",
    "# 前向传播， 通过输入，查找到输出的过程\n",
    "z = x @ w.t()\n",
    "# 激活函数，将输出合理化，比如需要计算概率就需要控制在0-1\n",
    "o = torch.sigmoid(z)\n",
    "\n",
    "# 定义目标值\n",
    "target = torch.ones(1, 2)\n",
    "\n",
    "# 计算均方误差损失\n",
    "loss = F.mse_loss(o, target)\n",
    "\n",
    "# 反向传播,计算权重的梯度，用于下降\n",
    "loss.backward()\n",
    "\n",
    "# 打印损失值和权重 w 的梯度，loss 关于 w 的偏导（梯度）只和 x 以及 o 有关。也就是说，loss和输入与激活函数有关\n",
    "print(\"Loss:\", loss.item())\n",
    "print(\"Gradient of w:\", w.grad)"
   ],
   "id": "2970a5c96da3e05e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loss: 0.004980165511369705\n",
      "Gradient of w: tensor([[ 0.0036, -0.0002,  0.0036,  0.0010,  0.0018,  0.0017, -0.0024, -0.0014,\n",
      "          0.0038, -0.0034],\n",
      "        [ 0.0156, -0.0007,  0.0156,  0.0042,  0.0076,  0.0074, -0.0104, -0.0059,\n",
      "          0.0164, -0.0144]])\n"
     ]
    }
   ],
   "execution_count": 1
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
